<template>
	<view>
		<button @click="backHome()">返回到首页</button>
		<view>count:{{count}}</view>
		<view>
			<view class="title">监听</view>
			<view>监听值类型有nval和oval</view>
			<view>监听引用类型有nval</view>
			<view>监听引用类型需要deep:true 深度监听</view>
			<view>
				<text>{{person.name}}</text>
				<text @click="person.age++">{{person.age}}</text>
			</view>
		</view>
		<view>
			<view class="title">计算coumputed</view>
			价格：{{price}}
			数量：
			<button @click="addNum">{{num}}</button>
			<view>
				总价：{{total}}
			</view>
		</view>

	</view>
</template>

<script>
	var app = getApp();
	export default {
		onLoad(option) {
			this.count = option.count;
			uni.setNavigationBarTitle({
				title: option.title
			})
		},
		data() {
			return {
				price: 5,
				num: 6,
				person: {
					name: "邢帅兵",
					age: 22
				}
			}
		},
		watch: {
			"num": {
				handler(nval, oval) {
					console.log("数据num,从" + oval + "变成" + nval);
					localStorage.setItem("num", this.num)
					localStorage.setItem("total", this.total)
				}
			},
			// 对象都需深度监听
			"person": {
				handler(nval) {
					console.log(nval.name + "又变了" + nval.age)
				},
				deep: true, //深度监听
			}
		},
		computed: {
			total() {
				return this.price * this.num
			}
		},
		methods: {
			backHome() {
				var pages = getCurrentPages();
				console.log(pages);
				uni.navigateBack({
					detal: pages.length
				})
			},
			addNum() {
				app.globalData.num++;
				this.num = app.globalData.num
			}
		},
		onShow() {
			this.num = app.globalData.num
		},
	}
</script>

<style>

</style>
